package com.sohoometer;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ScheduledThreadPoolExecutor;

import org.java_websocket.WebSocket;


public class Main {
	public static void main(String[] args) throws Exception {
		int port = 6666;
		WebSocketDemo webSocketDemo = new WebSocketDemo(port);
		webSocketDemo.start();
		HashMap<Integer, WebSocket> maps = webSocketDemo.getMaps();
		Timer timer = new Timer();
		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				Set<Entry<Integer,WebSocket>> entrySet = maps.entrySet();
				Iterator<Entry<Integer, WebSocket>> iterator = entrySet.iterator();
				while(iterator.hasNext()) {
					Entry<Integer, WebSocket> next = iterator.next();
					WebSocket webSocket = next.getValue();
					if(!webSocket.isClosed()) {
						Date date = new Date();
						DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						webSocket.send("现在时间是"+dateFormat.format(date));
					}
				}
			}};
			timer.schedule(task, 10000, 10000);
	}
}
